/*
 * Created by wangjunjun on 2024/3/21.
 * @Author       : wangjunjun
 * @Date         : 2024-03-21
 * @brief        :
 *               1.检测本地是否存在需要上传的文件
 *               2.检测与服务器的联通状态
 *               3.目前存储服务ftp端口2121
 *               4.采用观察者模式去监控
 * @copyleft Apache 2.0
 * @version: V1.0.0
 */
#ifndef MONITOR_H
#define MONITOR_H
#include <string>
#include <vector>
#include <thread>
#include <mutex>
constexpr int MAX_RETRIES = 6; // 最大的连接次数
class Observer {
public:
    virtual void update(bool isOnline, bool hasFiles) = 0;
    virtual ~Observer() {}
};

class CMonitor {
public:
    CMonitor();
    ~CMonitor();
public:
    void attach(Observer* observer);
    void detach(Observer* observer);
    void notify(bool isOnline, bool hasFiles);
    void startMonitoring(const std::string& serverAddress, const int  serverPort, const std::string& directoryPath);
    void stopMonitoring();
private:
    void monitorDirectory();
    bool checkDirectoryFilesRecursive(const std::string& path);
    bool checkServerStatus();
private:
    std::string m_directoryPath;
    std::string m_serverAddress;
    bool m_stop;
    int m_ftpPort; //ftp服务端，端口
    std::thread monitorThread;
    std::vector<Observer*> observers;
    std::mutex m_mtx;
};
//监控状态判断类
class CMonitorStatus:public Observer{
public:
    CMonitorStatus():m_MonitorStatus(false){

    }
    ~CMonitorStatus(){

    }
    void update(bool isOnline, bool hasFiles) override;
    bool  GetMonitorStatue() const;
private:
    bool m_MonitorStatus;
};

#endif //MONITOR_H
